package com.example.petadopt.ui.adapter;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.petadopt.R;
import com.example.petadopt.common.Result;
import com.example.petadopt.dao.PetDao;
import com.example.petadopt.entity.Adopt;
import com.example.petadopt.entity.Pet;
import com.example.petadopt.ui.activity.PetDetailActivity;
import com.gzone.university.utils.GlideUtils;

import java.util.List;

public class AdoptApplicationAdapter extends RecyclerView.Adapter<AdoptApplicationAdapter.ViewHolder> {

    private List<Adopt> adoptList;

    public AdoptApplicationAdapter(List<Adopt> adoptList) {
        this.adoptList = adoptList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_adopt_application, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Adopt adopt = adoptList.get(position);

        // 获取宠物信息
        Result<Pet> petResult = PetDao.getPetById(adopt.getPetId());
        if (petResult.isSuccess()) {
            Pet pet = petResult.getData();

            // 设置宠物名称
            holder.tvPetName.setText(pet.getName());

            // 设置宠物信息
            String petInfo = pet.getBreed() + " · " + pet.getAge() + "岁";
            holder.tvPetInfo.setText(petInfo);

            // 设置申请时间
            holder.tvApplyTime.setText("申请于 " + adopt.getAdoptTime());

            // 设置宠物图片
            if (pet.getImage() != null && !pet.getImage().isEmpty()) {
                GlideUtils.load(holder.ivPetImage, pet.getImage());
            }
        }

        // 设置查看详情点击事件
        holder.btnViewDetail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 跳转到宠物详情页面
                Intent intent = new Intent(v.getContext(), PetDetailActivity.class);
                intent.putExtra("pet_id", adopt.getPetId());
                v.getContext().startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return adoptList.size();
    }

    /**
     * 更新数据
     */
    public void updateData(List<Adopt> newAdoptList) {
        this.adoptList = newAdoptList;
        notifyDataSetChanged();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView ivPetImage;
        TextView tvPetName;
        TextView tvPetInfo;
        TextView tvApplyTime;
        Button btnViewDetail;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ivPetImage = itemView.findViewById(R.id.iv_pet_image);
            tvPetName = itemView.findViewById(R.id.tv_pet_name);
            tvPetInfo = itemView.findViewById(R.id.tv_pet_info);
            tvApplyTime = itemView.findViewById(R.id.tv_apply_time);
            btnViewDetail = itemView.findViewById(R.id.btn_view_detail);
        }
    }
} 